<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-button
        type="primary"
        style="width: 120px"
        @click="dialogFormVisible = true"
      >添加工作模式</el-button>
      <el-table
        v-loading="loading"
        :data="workPatterns"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="id" label="工作模式编号" width="210" />
        <el-table-column prop="name" label="工作模式" width="206" />
        <el-table-column prop="workPatternTypeDesc" label="类型" width="206" />
        <el-table-column prop="workDate" label="工作日期" width="206" />
        <el-table-column label="工作时间" width="234">
          <template #default="{ row }">
            {{
              ((row.workEndMinute1 - row.workStartMinute1) / 60).toFixed(2)
            }}小时
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" width="165">
          <template #default="{row}">
            <el-button type="text" style="color: #419eff" @click="editWorkArrange(row)">编辑</el-button>
            <div class="line" />
            <el-button type="text" style="color: #f56c6c" @click="delWorkArrange(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="fnClose">
      <el-tabs v-model="tabs" @tab-click="handleClick">
        <el-tab-pane v-if="hide !=='连续制'" label="礼拜制" name="first" @click="type = '礼拜制'" />
        <el-tab-pane v-if="hide !=='礼拜制'" label="连续制" name="second" @click="type = '连续制'" />
      </el-tabs>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="工作模式名称:" prop="name">
          <el-input v-model="form.name" placeholder="请填写工作模式名称" />
        </el-form-item>
        <el-form-item
          :style="{ marginBottom: form.workPatternType == '1' ? 0 : '38px' }"
          label="连续工作天数:"
          prop="workDay"
        >
          <el-checkbox-group v-if="form.workPatternType == '1'" v-model="form.workDay">
            <el-checkbox label="monday" name="周一">周一</el-checkbox>
            <el-checkbox label="tuesday" name="周二">周二</el-checkbox>
            <el-checkbox label="wednesday" name="周三">周三</el-checkbox>
            <el-checkbox label="thursday" name="周四">周四</el-checkbox>
            <el-checkbox label="friday" name="周五">周五</el-checkbox>
            <el-checkbox label="saturday" name="周六">周六</el-checkbox>
            <el-checkbox label="sunday" name="周日">周日</el-checkbox>
          </el-checkbox-group>
          <template v-else>
            上
            <el-input v-model="form.workDayNum" style="width:110px;margin-left:10px;margin-right:21px" placeholder="请输入"><span slot="suffix" style="color: #000">天</span></el-input>
            休
            <el-input v-model="form.restDayNum" style="width:110px;margin-left:10px" placeholder="请输入"><span slot="suffix" style="color: #000">天</span></el-input>
          </template>
        </el-form-item>
        <div v-if="form.workPatternType == '1'" class="tips" style="margin-bottom: 18px">
          选中为工作时间，未选中为休息时间
        </div>
        <el-form-item prop="time" label="工作时间:">
          <el-time-picker
            v-model="form.time"
            clearable
            is-range
            format="HH:mm"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            placeholder="选择时间范围"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button
          type="primary"
          @click="fnCommit"
        >确 定</el-button>
        <el-button @click="fnClose">取 消</el-button>
      </div>
    </el-dialog>
    <Footer />
  </div>
</template>

<script>
import { addWorkPatternAPI, delWorkPatternAPI, editWorkPatternAPI, getWorkPatternPageAPI } from '@/api/employee'
export default {
  name: 'WorkPatternSetting',
  data() {
    return {
      workPatterns: [],
      page: 1,
      pageSize: 10,
      counts: 0,
      loading: false,
      dialogFormVisible: false,
      form: {
        name: '',
        workEndMinute1: '',
        workStartMinute1: '',
        workPatternType: '1',
        workDayNum: '', // 连续工作天数
        restDayNum: '', // 休息天数
        monday: 2,
        tuesday: 2,
        wednesday: 2,
        thursday: 2,
        friday: 2,
        saturday: 2,
        sunday: 2,
        workDay: [],
        time: [new Date(0, 0, 0, 9, 0), new Date(0, 0, 0, 18, 0)]
      },
      tabs: 'first',
      rules: {
        name: [{ required: true, message: '工作模式名称不能为空', trigger: 'blur' }],
        workDay: [{ validator: (rule, value, callback) => {
          if (this.form.workDayNum === '' || this.form.restDayNum === '') {
            callback(new Error('请填写连续工作天数'))
          } else {
            callback()
          }
        }, trigger: 'blur', required: true }],
        time: [{ required: true, message: '请填写工作时间', trigger: 'blur' }]
      },
      hide: '',
      title: '新增工作模式'
    }
  },
  created() {
    this.getWorkPatterns()
  },
  methods: {
    // 获取工作模式列表
    async getWorkPatterns() {
      this.loading = true
      const res = await getWorkPatternPageAPI({
        page: this.page,
        pageSize: this.pageSize
      })
      this.workPatterns = res.items
      this.counts = res.counts * 1
      this.loading = false
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.page = 1
      this.getWorkPatterns()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getWorkPatterns()
    },
    // 点击标题
    handleClick(tab, event) {
      this.$refs.form.resetFields()
      if (tab['_props'].label === '连续制') {
        this.form.workPatternType = '2'
      } else {
        this.form.workPatternType = '1'
      }
      this.form.workDayNum = ''
      this.form.restDayNum = ''
    },
    // 提交
    fnCommit() {
      if (this.title === '新增工作模式') {
        this.addWorkArrange()
      } else {
        this.editCommitWorkArrange()
      }
    },
    // 添加工作模式
    async addWorkArrange() {
      this.form.workDay.forEach(day => {
        this.form[day] = 1
      })
      this.form.workStartMinute1 = this.form.time[0].getHours() * 60 + this.form.time[0].getMinutes()
      this.form.workEndMinute1 = this.form.time[1].getHours() * 60 + this.form.time[1].getMinutes()
      await addWorkPatternAPI(this.form)
      this.fnClose()
      this.$message.success('添加成功')
      this.getWorkPatterns()
    },
    // 修改工作模式
    editWorkArrange(row) {
      this.title = '修改工作模式'
      this.hide = row.workPatternType === 1 ? '礼拜制' : '连续制'
      this.tabs = row.workPatternType === 1 ? 'first' : 'second'
      this.form = { ...row }
      this.$set(this.form, 'workDay', [])
      this.$set(this.form, 'time', [])
      if (row.workPatternType === 1) {
        const array = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
        array.forEach(item => {
          if (this.form[item] === 1) {
            this.form.workDay.push(item)
          }
        })
      }
      this.form.time = [new Date(0, 0, 0, parseInt(row.workStartMinute1 / 60), row.workStartMinute1 % 60), new Date(0, 0, 0, parseInt(row.workEndMinute1 / 60), row.workEndMinute1 % 60)]
      this.dialogFormVisible = true
    },
    // 修改提交
    async editCommitWorkArrange() {
      const array = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
      array.forEach(day => {
        if (this.form.workDay.includes(day)) {
          this.form[day] = 1
        } else {
          this.form[day] = 2
        }
      })
      this.form.workStartMinute1 = this.form.time[0].getHours() * 60 + this.form.time[0].getMinutes()
      this.form.workEndMinute1 = this.form.time[1].getHours() * 60 + this.form.time[1].getMinutes()
      await editWorkPatternAPI(this.form)
      this.fnClose()
      this.$message.success('修改成功')
      this.getWorkPatterns()
    },
    // 关闭弹窗
    fnClose() {
      this.dialogFormVisible = false
      this.title = '新增工作模式'
      this.hide = ''
      this.tabs = 'first'
      this.form = {
        name: '',
        workEndMinute1: '',
        workStartMinute1: '',
        workPatternType: '1',
        workDayNum: '', // 连续工作天数
        restDayNum: '', // 休息天数
        monday: 2,
        tuesday: 2,
        wednesday: 2,
        thursday: 2,
        friday: 2,
        saturday: 2,
        sunday: 2,
        workDay: [],
        time: [new Date(0, 0, 0, 9, 0), new Date(0, 0, 0, 18, 0)]
      }
    },
    // 删除工作模式
    async delWorkArrange(delId) {
      await delWorkPatternAPI(delId)
      this.$message.success('删除成功')
      this.getWorkPatterns()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .el-card {
    min-width: 1286px;
    :deep(.el-card__body) {
      padding: 28px;
      .el-table {
        border: 1px solid #ebeef5;
        margin-top: 21px;
        .el-table__header th {
          color: #818693;
          background-color: #f8faff;
          &:nth-child(1) .cell {
            padding-left: 20px;
          }
          &:nth-last-child(2) .cell {
            text-align: center;
          }
        }
        .el-table__body td {
          font-size: 13px;
          color: #20232a;
          font-family: "Microsoft YaHei";
          &:nth-child(1) .cell {
            padding-left: 20px;
          }
          &:last-child .cell {
            text-align: center;
            .line {
              display: inline-block;
              width: 1px;
              height: 1em;
              margin: 0 8px;
              background-color: #dcdfe6;
              vertical-align: middle;
            }
            .el-button {
              font-size: 13px;
            }
          }
        }
      }
      .el-pagination {
        text-align: center;
        margin-top: 30px;
      }
    }
  }
  :deep(.el-dialog) {
    border-radius: 8px;
    width: 700px;
    overflow: hidden;
    .el-dialog__header {
      background-color: #f3f4f7;
      padding: 15px 30px 15px;
    }
    .el-dialog__body {
      padding: 30px 60px;
      .el-tabs {
        margin-bottom: 15px;
        .el-tabs__nav {
          transform: translateX(30px) !important;
        }
        .is-active {
          font-size: 16px;
          font-weight: 600;
          color: #000;
        }
      }
      .el-form {
        .el-form-item__label {
          min-width: 7em;
        }
        .el-form-item__content {
          display: flex;
          .el-input__suffix{
            right: 1em;
          }
          .el-date-editor{
            flex: 1;
          }
        }
        .el-checkbox {
          margin-right: 12px;
        }
        .tips {
          color: #bec4d0;
          padding-left: 100px;
        }
      }
    }
  }
}
</style>
